package com.rewa.test.config;

import com.rewa.test.interceptor.AuthenticationInterceptor;
import com.rewa.test.interceptor.ResponseResultInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer{

	public static String ALLPATH = "/**";
	@Autowired
	private ResponseResultInterceptor responseResultInterceptor;

	@Autowired
	private AuthenticationInterceptor authenticationInterceptor;


	@Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 对所有的请求都要拦截，但是“/getInt”不在拦截的范围之内
		// 排除swagger的，否则访问不了swagger-ui的web页面，会有弹框弹出
		registry.addInterceptor(authenticationInterceptor);
        registry.addInterceptor(responseResultInterceptor).addPathPatterns(ALLPATH)
				.excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");
    }

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		//其他静态资源
		registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
		//swagger增加url映射
		registry.addResourceHandler("swagger-ui.html")
				.addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars/**")
				.addResourceLocations("classpath:/META-INF/resources/webjars/");
	}
}
